package edu.its.dreamteam.web.pages.admin;

import org.apache.tapestry.ValueEncoder;
import org.apache.tapestry.annotations.Component;
import org.apache.tapestry.annotations.Persist;
import org.apache.tapestry.corelib.components.Form;
import org.apache.tapestry.ioc.annotations.Inject;

import edu.its.dreamteam.daf.beans.Position;
import edu.its.dreamteam.daf.dao.IPositionDao;
import edu.its.dreamteam.web.base.SecureBasePage;
import edu.its.dreamteam.web.util.StringValueEncoder;

public class PositionForm extends SecureBasePage {
	@Inject
	private IPositionDao positionDao;
	
	@Persist
	private Position position;
	

	@Component
	private Form positionForm;	
	private String idPosition;

	

	public Position getPosition() {
		return position;
	}

	public void setPosition(Position position) {
		this.position = position;
	}

	public IPositionDao getPositionDao() {
		return positionDao;
	}

	public ValueEncoder<String> getValueEncoder(){
		return new StringValueEncoder();
	} 
	
	void onPrepare(){
		if(position == null){
			position = new Position();
		}
	}
	
	void cleanupRender() {
		setPosition(null);	
	}
	Object onSuccessFromPositionForm(){
		if(getPosition().getIdPosition()==null){
			
			getPositionDao().insert(getPosition());
		}else{
			getPositionDao().update(getPosition());			
		}
		setPosition(null);	
		return PositionGrid.class;
	}
	
	void onActionFromClear(){
		setPosition(null);	
		positionForm.clearErrors();
	}
	
	void onActivate(String idPosition){
		this.idPosition = idPosition;
		setPosition((Position) getPositionDao().findByPrimaryKey(idPosition));	
	}
	
	String onPassivate(){ 
		return idPosition;
	}	
	
	
}
